package com.ask.controller;

import cn.hutool.core.util.StrUtil;
import com.ask.constants.SystemConstants;
import com.ask.dto.Result;
import com.ask.entity.Shop;
import com.ask.service.businessService.IShopService;
import com.ask.utils.CacheClient;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shop")
@Slf4j
public class ShopController {

    @Autowired
    private IShopService shopService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private CacheClient cacheClient;

    @GetMapping("/of/type")
    public Result queryShopByType(
            @RequestParam("typeId") Integer typeId,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "x", required = false) Double x,
            @RequestParam(value = "y", required = false) Double y) {
//        Page<Shop> page = shopService.query()
//                .eq("type_id", typeId)
//                .page(new Page<>(current, 5));
//        return Result.success(page.getRecords());
        return shopService.queryShopByType(typeId, current, x, y);
    }

    /**
     * 根据id获取店铺信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        return shopService.queryById_WithMutex(id);
        //return shopService.queryById_normal_handling(id);
    }

    /**
     * 跟新店铺信息
     *
     * @param shop
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Shop shop) {
        return shopService.updateWithCache(shop);
    }

    @GetMapping("/of/name")
    public Result queryShopByName(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "current", defaultValue = "1") Integer current) {
        Page<Shop> page = shopService.query()
                .like(StrUtil.isNotBlank(name), "name", name)
                .page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        return Result.success(page.getRecords());
    }




}
